package Year21_Month10.day1030;

public class Queue<E>{
    public static class ListNode<E>{

        ListNode<E> next;
        ListNode<E> pre;
        E val;

        ListNode(E val){
            this.val = val;
        }
    }
    ListNode<E> front;
    ListNode<E> rear;
    int size = 0;

    //判断是否为空
    public boolean isEmpty(){
        return 0 == size;
    }
    public int size(){
        return size;
    }
    //入队列
    public void offer(E e){
        ListNode<E> newNode = new ListNode<>(e);
        if(isEmpty()){
            front = newNode;
        }else{
            rear.next = newNode;
            newNode.pre = rear;
        }
        rear = newNode;
        size++;
    }

    //出队列
    public E poll(){
        E key = null;
        if(isEmpty()){
            return  null;
        }
        if(size == 1){
            rear = null;
            front = null;
        }
        key = rear.val;
        rear = rear.pre;
        rear.next.pre = null;
        rear.next = null;
        size--;
        return key;
    }
    //获取队头
    public E peek(){
        return front.val;
    }

    public static void main(String[] args) {
        Queue<Integer> queue = new Queue<>();
        queue.offer(1);
        queue.offer(2);
        queue.offer(3);
        System.out.println(queue.size());
        System.out.println(queue.peek());

        queue.poll();
        System.out.println(queue.peek());
    }
}
